package com.heima.selection.core.base;


import com.heima.selection.core.enums.RetEnum;
import com.heima.selection.core.exception.BusinessException;
import lombok.ToString;

import java.io.Serializable;


/**
 * 响应结果返回
 *
 * @author YuanZiwen
 * @date 2025/06/22
 */
@ToString
@SuppressWarnings("all")
public class Ret<T> implements Serializable {


    /**
     * 是否成功
     */
    private boolean success;
    /**
     * 编码
     */
    private String code;
    /**
     * 消息
     */
    private String msg;
    /**
     * 结果体
     */
    private T data;

    /**
     * 带数据的返回
     *
     * @param data
     */
    public Ret(T data) {
        super();
        this.data = data;
    }

    /**
     * 带指定状态码和信息的返回
     *
     * @param code
     * @param msg
     */
    public Ret(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 带指定状态码、信息和处理结果的返回
     *
     * @param success
     * @param code
     * @param msg
     */
    public Ret(boolean success, String code, String msg) {
        super();
        this.success = success;
        this.code = code;
        this.msg = msg;
    }

    /**
     * 带数据、指定状态码、信息的返回
     *
     * @param code
     * @param msg
     * @param data
     */
    public Ret(String code, String msg, T data) {
        super();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 带数据、指定状态码、信息和处理结果的返回
     *
     * @param success
     * @param code
     * @param msg
     * @param data
     */
    public Ret(boolean success, String code, String msg, T data) {
        super();
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Ret() {

    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isOk() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }


    /**
     * 带参数的返回结果
     *
     * @param t
     * @return {@code Ret<T> }
     */
    public static <T> Ret<T> buildOk(T t) {
        Ret<T> result = new Ret<T>(t);
        result.setSuccess(true);
        result.setCode(RetEnum.SUCCESS.getCode());
        result.setMsg(RetEnum.SUCCESS.getMessage());
        return result;
    }


    /**
     * 有参数的返回失败操作
     *
     * @param t
     * @return {@code Ret<T> }
     */
    public static <T> Ret<T> buildError(T t) {
        Ret<T> result = new Ret<T>(t);
        result.setSuccess(false);
        result.setCode(RetEnum.FAIL.getCode());
        result.setMsg(RetEnum.FAIL.getMessage());
        return result;
    }


    /**
     * 无参数的返回结果
     *
     * @return {@code Ret<String> }
     */
    public static Ret<String> buildOk() {
        Ret<String> result = new Ret<String>();
        result.setSuccess(true);
        result.setCode(RetEnum.SUCCESS.getCode());
        result.setMsg(RetEnum.SUCCESS.getMessage());
        return result;
    }


    /**
     * 无参数的 返回失败操作
     *
     * @return {@code Ret<String> }
     */
    public static Ret<String> buildError() {
        Ret<String> result = new Ret<String>();
        result.setSuccess(false);
        result.setCode(RetEnum.FAIL.getCode());
        result.setMsg(RetEnum.FAIL.getMessage());
        return result;
    }

    /**
     * @return ResponseResult<String> 返回类型
     * @Title: failResponse
     * @Description:无参数的 返回失败操作
     */
    public static Ret<String> buildError(String mesg) {
        Ret<String> result = new Ret<String>();
        result.setSuccess(false);
        result.setCode(RetEnum.FAIL.getCode());
        result.setMsg(mesg);
        return result;
    }

    /**
     * @param code
     * @param mesg
     * @return ResponseResult<?> 返回类型
     * @Title: failResponse
     * @Description: 返回失败操作
     */
    public static Ret<String> buildError(String code, String mesg) {
        Ret<String> result = new Ret<String>();
        result.setSuccess(false);
        result.setCode(code);
        result.setMsg(mesg);
        return result;
    }

    /**
     * @param e
     * @return ResponseResult<?> 返回类型
     * @Title: failResponse
     * @Description: 处理异常
     */
    public static Ret<?> buildError(BusinessException e) {
        Ret<String> result = new Ret<String>();
        result.setSuccess(false);
        result.setCode(e.getCode());
        result.setMsg(e.getMessage());
        return result;
    }

    /**
     * @param <T>
     * @param code
     * @param mesg
     * @param t
     * @return ResponseResult<T> 返回类型
     * @Title: createResponseResult
     * @Description: 创建返回结构
     */
    public static <T> Ret<T> build(String code, String mesg, T t) {
        Ret<T> result = new Ret<T>(code, mesg, t);
        return result;
    }

    /**
     * @param <T>
     * @param result void 返回类型
     * @Title: getResult
     * @Description:获取结果对象
     */
    public static <T> T get(Ret<T> result) throws BusinessException {
        if (!RetEnum.SUCCESS.getCode().equals(result.getCode())) {
            throw new BusinessException(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    /**
     * @param <T>
     * @param result
     * @return Boolean 返回类型
     * @Title: isSuccess
     * @Description: 是否成功
     */
    public static <T> Boolean isOk(Ret<T> result) {
        return RetEnum.SUCCESS.getCode().equals(result.getCode());
    }

    /**
     * @param <T>
     * @param result
     * @return Boolean 返回类型
     * @Title: isFail
     * @Description: 是否失败
     */
    public static <T> Boolean isError(Ret<T> result) {
        return RetEnum.FAIL.getCode().equals(result.getCode());
    }
}
